package com.shuqing.interceptor;

import com.shuqing.domain.User;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class ResourceInterceptor extends HandlerInterceptorAdapter {
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        User crurrentUser = (User) request.getSession().getAttribute("USER_SESSION");
//        如果当前的session中保留的user信息 那么代表你已经登录了 直接进入页面 返回 true
        if(crurrentUser != null){
            return true;
        }
//        获取URL完整路径
        String uri = request.getRequestURI();
//        判断是否是登陆页面的请求
        if (uri.indexOf("login") > 0) {
            return true;
        }
        request.setAttribute("msg", "您还没有登录，请先登录！");
        request.getRequestDispatcher("/admin/login.jsp").forward(request, response);
        return false;
    }
}
